package com.yixing.tech.entity.entity.attr;

import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.base.ConditionsVO;
import com.yixing.tech.common.vo.s5.EntitySchema;
import com.yixing.tech.entity.service.EntityBaseService;
import org.springframework.data.mongodb.core.MongoTemplate;

/**
 * @author yixing tech
 * at 2021/10/9
 * describe：
 */
public class SwitchText extends AttrType {
    public SwitchText() {
        super("布尔值", "switchText");
    }

    @Override
    public Object wrapValue(EntitySchema.AttrVO attr, Object value, MongoTemplate mongoTemplate, EntityBaseService objService, String tenantId) throws BusinessException {
        if("开".equals(value) || "是".equals(value) || Boolean.TRUE.equals(value) || "1".equals(value)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 数据插入处理
     *
     * @param source
     * @param attr
     * @return
     * @throws BusinessException
     */
    @Override
    public Object in(Object source, EntitySchema.AttrVO attr) throws BusinessException {
        try {
            source = Boolean.valueOf(source.toString());
        } catch (Exception e) {
            source = false;
        }
        return source;
    }

    /**
     * 过滤条件处理
     *
     * @param cond
     * @param attr
     */
    @Override
    public void condition(ConditionsVO.Condition cond, EntitySchema.AttrVO attr) throws BusinessException {
        super.condition(cond, attr);
        boolean v = Boolean.parseBoolean(cond.getValue().toString());
        cond.setValue(v);
    }
}
